/*
 * 	tip窗口可以设置为在调用冬帽的top、right、bottom、left显示和窗口中间显示。默认为中间显示。
 * 	背景颜色取的Maxtips.default默认的背景颜色。可以通过$.MaxTipformat(opt)方法重置Maxtips.default的默认参数。
 * 
 *	Ahthor：MaxJh
 * 	Copyright：MaxJh
 * 	Version：maxModal-tip-0.1.js
 * */
+(function($){
	var Maxtips = {
		init:function(obj,config){
			this.tar = obj;
			this.config = $.extend(true, Maxtips.defaults, config);
			this.tipDom =	$("<div class='maxModal_tip'></div>");
			this.tipHead = $("<div class='maxModal_tip_head'></div>");
			this.tipSide = $("<div class='maxModal_side maxModal_side_"+this.config.side+"'></div>");
			this.tipCon = $("<div class='maxModal_tip_content'></div>")
			this.timer = "";
			this.creatTip();
		},
		creatTip:function(){
			this._addStyle();
			this._template();
			this._position();
		},
		//初始化模版
		_template:function(){
			var config = this.config,
				tipDom = this.tipDom,
				tipSide = this.tipSide,
				tipCon = this.tipCon;
			if($(".maxModal_tip").length > 0){
				//如果存在延时事件，清楚延迟
				if(this.timer){
					clearTimeout(this.timer);
					this.timer = "";
				}
				//移除该dom
				$(".maxModal_tip").remove();
			//	this.tipDom.remove();
			};
			tipCon.append(config.content);
			//中心显示tip窗口
			if(config.side == "center"){
				this.tipHead.append(config.title)
				tipDom.append(this.tipHead);
			}
			tipDom.append(tipSide);
			tipDom.append(tipCon);
			this._initSty();
		//	this._event();
			$("body").append(tipDom);
			this._close();
			this._event();
		},
		_addStyle:function(){
			var config = this.config;
			var cssDom = $(".Max_tipsStyle");
			var headDom = $(document).find("head");
			if(cssDom.length == 0){
				var stys = "<style class='Max_tipsStyle' type='text/css'>"+
							".maxModal_tip {max-width:80%;max-height:90%;position: absolute;top:0;left: 10px;background: "+Maxtips.defaults.background+";color:"+Maxtips.defaults.color+";border-radius: 3px;z-index:1000;}"+
							".maxModal_tip .maxModal_tip_head {padding:  6px 12px;background: "+Maxtips.defaults.titbg+";border-top-left-radius: 3px;border-top-right-radius: 3px;color:"+Maxtips.defaults.color+";}"+
				  		".maxModal_tip .maxModal_side {position: absolute;vertical-align: middle;width: 0;height: 0;border-width: 10px;}"+
				  		".maxModal_tip .maxModal_side {position: absolute;vertical-align: middle;width: 0;height: 0;border-width: 10px;}"+
				  		".maxModal_tip .maxModal_side_left {top: 50%;margin-top: -10px;right: 0px;margin-right: -20px;border-style: dashed dashed dashed solid;border-color: transparent transparent transparent "+Maxtips.defaults.background+";}"+
				  		".maxModal_tip .maxModal_side_right {top: 50%;margin-top: -10px;left: 0px;margin-left: -20px;border-style: dashed solid dashed dashed;border-color: transparent "+Maxtips.defaults.background+" transparent transparent;}"+
				  		".maxModal_tip .maxModal_side_bottom {top: 0;margin-top: -20px;left: 50%;margin-left: -10px;border-style: dashed dashed solid dashed;border-color: transparent transparent "+Maxtips.defaults.background+" transparent;}"+
				  		".maxModal_tip .maxModal_side_top {bottom: 0;margin-bottom: -20px;left: 50%;margin-left: -10px;border-style: solid dashed dashed dashed;border-color: "+Maxtips.defaults.background+" transparent transparent transparent;}"+
				  		".maxModal_tip_content {width:100%;height:100%;padding: 6px 12px;box-sizing: border-box;color:"+Maxtips.defaults.color+";}"+
							"</style>";
				headDom.append(stys);
			}
		},
		//tips定位
		_position:function(){
			var config = this.config,
				tipDom = this.tipDom,
				tipSide = this.tipSide,
				tipCon = this.tipCon;
			
			var win_W = $("body").width();
			var win_H = $("body").height();
			
			var tar = this.tar;//调用的jquery对象
			var xy = tar.getXY();//调用dom对象距离文档顶部和左部的距离
			var wh = tar.getWH();//调用dom对象的宽高
			
			var tipWH = tipDom.getWH();//tip的宽高
			var _left_ = 0,
				_top_ = 0,
				mark = "";
			if(config.side == "right"){//tooltip显示的位置
				_left_ = xy.left+wh.width + 10;
				_top_ = xy.top + ((wh.height - tipWH.height) / 2);
				if( (_left_ + tipWH.width > win_W) && (xy.left - tipWH.width > 0)){
					mark = "left";
				}
			}else if(config.side == "left"){
				_left_ = xy.left - tipWH.width - 10;
				_top_ = xy.top + ((wh.height - tipWH.height) / 2);
				if(_left_ - tipWH.width < 0){
					mark = "right";
				}
			}else if(config.side == "top"){
				_left_ = xy.left + ( (wh.width - tipWH.width) / 2);
				_top_ = xy.top -  tipWH.height - 10;
				if(_top_ - tipWH.height < 0){
					mark = "bottom";
				}
			}else if(config.side == "bottom"){
				_left_ = xy.left + ( (wh.width - tipWH.width) / 2);
				_top_ = xy.top +  wh.height + 10;
				if(_top_ + tipWH.height < win_H){
					mark = "top";
				}
			}else{//tip窗口中心显示
				_left_ =	($(window).width() - tipWH.width) / 2 ; 
				_top_ = 	($(window).height() - tipWH.height) / 2 ;
	//			console.log(_left_+"=="+_top_)
			};
			
			switch(mark){
					case "right": 
								  //opts._initCaret(tipDom,opts);
								  tipSide.removeClass("maxModal_side_left").addClass("maxModal_side_right");
								  _left_ = xy.left+wh.width + 10;
									_top_ = xy.top + ((wh.height - tipWH.height) / 2);
								  break;
					case "left" : //opts._initCaret(tipDom,opts);
									tipSide.removeClass("maxModal_side_right").addClass("maxModal_side_left");
									 _left_ = - tipWH.width + 10;
									_top_ = xy.top + ((wh.height - tipWH.height) / 2);
								  break;
					case "top" : //opts._initCaret(tipDom,opts);
									tipSide.removeClass("maxModal_side_bottom").addClass("maxModal_side_top");
									_left_ = xy.left + ( (wh.width - tipWH.width) / 2);
									_top_ = xy.top -  tipWH.height - 10;
								  break;
					case "bottom" : //opts._initCaret(tipDom,opts);
									tipSide.removeClass("maxModal_side_top").addClass("maxModal_side_bottom");
									_left_ = xy.left + ( (wh.width - tipWH.width) / 2);
									_top_ = xy.top +  wh.height + 10;
								  break;			  
			};
			
			tipDom.css({
				"left":_left_,
				"top":_top_
			});
			//重置位置
			this._resize();
		},
		_resize:function(){
			var tipDom = this.tipDom;
			var tar = this.tar;
			var side = this.config.side;
			var tarXY = tar.getXY();
			var tarWH = tar.getWH();
			var tipXY = tipDom.getXY();
			var tipWH = tipDom.getWH();
			if(side == "left" || side == "right"){
				tipDom.css({
					"top":tarXY.top + ((tarWH.height - tipWH.height) / 2)
				});
			}else if(side == "top" || side == "bottom"){
				tipDom.css({
					"left":tarXY.left + ( (tarWH.width - tipWH.width) / 2)
				});
			}
		},
		//关闭tip		
		_close:function(){
			if($(".maxModal_tip").length>0 && this.config.time){
				this.timer = setTimeout(function(){
					$(".maxModal_tip").remove();
				},this.config.time*1000);
			}
		},
		//tipDom绑定事件
		_event:function(){
			var tipDom = this.tipDom;
			var _this = this;
			tipDom.on("mouseenter",function(){
				clearTimeout(_this.timer);
			}).on("mouseleave",function(){
				_this._close();
			})
			tipDom.on("click",function(){
				this.remove();
			})
		},
		//根据参数初始化样式
		_initSty:function(){
			var config = this.config,
				side = this.config.side,
				tipDom = this.tipDom,
				tipHead = this.tipHead,
				tipSide = this.tipSide,
				tipCon = this.tipCon;
			if(config.width){
				tipDom.width(config.width);
			}
			if(config.height){
				tipDom.height(config.height);
			}
			if(config.borderRadius){
				tipDom.css({
					"border-radius":config.borderRadius
				});
				tipHead.css({
					"border-top-left-radius":config.borderRadius,
					"border-top-right-radius":config.borderRadius
				})
			}
			if(config.zIndex){
				tipDom.css({
					"z-index":config.zIndex
				});
			}
			if(side == "center"){
				tipDom.css({
					"position":"fixed"
				})
			}
		}
	};
	//默认参数
	Maxtips.defaults = {
		time:2,											//默认为2,2秒后自动隐藏。为0时，不自动隐藏。
		side:"center", 							//还可为top,right,bottom,left
		title:"标题",								//标题名
		titbg:"#09c",						//标题背景颜色
		color:"#ebebeb",										//字体颜色
		content:"请插入tip的内容！",	//内容
		width:0,										//side=center，设定tips的宽度有效，side !=center时，限制max_con的宽度
		height:0,										//side=center,设定内容的最小高度，默认为0
		borderRadius:0,							//倒圆角大小
		background:"#435061",				//背景颜色
		zIndex:0
	};

	//插件调用方法
	$.fn.MaxTip = function(config){
		return  Maxtips.init(this,config);
	};
	
	//暴露默认参数
	$.fn.MaxTip.defaults = Maxtips.defaults;
	//暴露默认参数
	$.MaxTipDefault =  Maxtips.defaults;

	
	//暴露插件默认参数初始化
	$.fn.MaxTip.format = function(config){
		$.extend(true,Maxtips.defaults,config);
	};
	//暴露插件默认参数初始化
	$.MaxTipformat = function(config){
		$.extend(true,Maxtips.defaults,config);
	};
	
	$.fn.getXY = function(){
		var _top = 0,_left = 0;
		var obj = $(this).get(0);
		while(obj){
			_top += obj.offsetTop;
			_left += obj.offsetLeft;
			obj = obj.offsetParent;
		};
		return {
			top:_top,
			left:_left
		}
	};
	
	$.fn.getWH = function(){
		var w = 0,h = 0;
		w = $(this).outerWidth();
		h = $(this).outerHeight();
		return {
			width:w,
			height:h
		}
	}
})(jQuery);